package io.renren.modules.task.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.HttpClientUtil;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.task.constant.ManagePublicMethod;
import io.renren.modules.task.entity.MtCcAgvCommunLogEntity;
import io.renren.modules.task.entity.MtCcEquipmentRunInfoEntity;
import io.renren.modules.task.entity.MtCcEquipmentRunInfoLogEntity;
import io.renren.modules.task.enums.MtCcAGVEnum;
import io.renren.modules.task.enums.MtCcEnum;
import io.renren.modules.task.model.AgvParam;
import io.renren.modules.task.model.MtCcAreaRouteEntityVo;
import io.renren.modules.task.model.Position;
import io.renren.modules.task.service.MtCcAgvCommunLogService;
import io.renren.modules.task.service.MtCcAreaRouteService;
import io.renren.modules.task.service.MtCcEquipmentRunInfoLogService;
import io.renren.modules.task.service.MtCcEquipmentRunInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static io.renren.modules.task.constant.ManageConstant.CALL_MATERIAL_ORDER_TYPE;


/**
 * @Author: weihongyang
 * @Date: 2019/5/24 10:13
 * @Version 1.0
 */
@RestController
@Api(description = "呼叫原料API接口")
@RequestMapping("/call/material")
public class CallMaterialController extends AbstractController {


    @Autowired
    private MtCcAreaRouteService mtCcAreaRouteService;

    @Autowired
    private MtCcEquipmentRunInfoLogService mtCcEquipmentRunInfoLogService;

    @Autowired
    private MtCcEquipmentRunInfoService mtCcEquipmentRunInfoService;

    @Autowired
    private MtCcAgvCommunLogService mtCcAgvCommunLogService;

    @PostMapping("/select/console/rounte")
    @ApiOperation(value = "操作台叫料操作", notes = "操作台叫料操作")
    public R selectConsoleRounteToAgv() throws Exception {
        Date date = new Date();
        Integer count = mtCcEquipmentRunInfoLogService.selectCountByTaskType(0) + 1;
        String reqCode = ManagePublicMethod.creatOrderCode(CALL_MATERIAL_ORDER_TYPE,
                String.valueOf(getUser().getConsoleCode()), String.valueOf(count));
        MtCcEquipmentRunInfoEntity mtCcEquipmentRunInfoEntity = new MtCcEquipmentRunInfoEntity();
        mtCcEquipmentRunInfoEntity.setCreateTime(date);
        mtCcEquipmentRunInfoEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcEquipmentRunInfoEntity.setUserId(getUser().getUserId().toString());
        mtCcEquipmentRunInfoEntity.setTaskName(MtCcEnum.CALLMATERIAL.getMsg());
        mtCcEquipmentRunInfoEntity.setTaskType(MtCcEnum.CALLMATERIAL.getCode());
        mtCcEquipmentRunInfoEntity.setStatus(0);
        mtCcEquipmentRunInfoEntity.setTaskCode(reqCode);
        mtCcEquipmentRunInfoEntity.setEnable(0);
        mtCcEquipmentRunInfoService.save(mtCcEquipmentRunInfoEntity);

        MtCcEquipmentRunInfoLogEntity mtCcEquipmentRunInfoLogEntity = new MtCcEquipmentRunInfoLogEntity();
        mtCcEquipmentRunInfoLogEntity.setCreateTime(date);
        mtCcEquipmentRunInfoLogEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcEquipmentRunInfoLogEntity.setUserId(getUserId().toString());
        mtCcEquipmentRunInfoLogEntity.setTaskName(MtCcEnum.CALLMATERIAL.getMsg());
        mtCcEquipmentRunInfoLogEntity.setTaskType(MtCcEnum.CALLMATERIAL.getCode());
        mtCcEquipmentRunInfoLogEntity.setTaskCode(reqCode);
        mtCcEquipmentRunInfoLogEntity.setEnable(0);
        mtCcEquipmentRunInfoLogEntity.setStatus(1);
        mtCcEquipmentRunInfoLogService.save(mtCcEquipmentRunInfoLogEntity);

        return R.ok();
    }

    @PostMapping("/select/material/table/rounte/one")
    @ApiOperation(value = "放料台确认操作1", notes = "放料台确认操作1")
    public R selectMaterialTableRounteOne(@RequestBody Map<String, Object> params) throws Exception {
        String url = MtCcAGVEnum.AGVURL_callMaterial.getCode();
//        Map<String,Object> params = new HashMap<>();
//        params.put("taskType","0");
//        params.put("status","0");
        List<MtCcAreaRouteEntityVo> list = mtCcAreaRouteService.selectRouteCodeByTaskType(params);
//        List<MtCcEquipmentRunInfoEntity> list1 = mtCcEquipmentRunInfoService.selectRunInfoByCreatTimeAsc();
        String reqCode = list.get(0).getTaskCode();
        String taskTyp = "HJYL";
//        Position[] positions = new Position[3];
//        positions[0].setPositionCode("Y1");
//        positions[0].setType("00");

        Position position = new Position();
        position.setPositionCode("1Y");
        position.setType("00");
        Position position1 = new Position();
        position1.setPositionCode(list.get(0).getSourceNo());
//        position.setPositionCode("204835AA203655");
        position1.setType("00");
        Position position2 = new Position();
        position2.setPositionCode(list.get(0).getDestinationNo());
//        position1.setPositionCode("282655AA228370");
        position2.setType("00");
        Object[] positionCodePath = {position, position1, position2};
        AgvParam agvParam = new AgvParam();
        agvParam.setReqCode(reqCode);
        agvParam.setTaskCode(reqCode);
        agvParam.setAgvCode("3002");
        agvParam.setTaskTyp(taskTyp);
        agvParam.setPositionCodePath(positionCodePath);
        String resultMsg = null;//获取服务端返回的参数
        String param = JSONObject.toJSONString(agvParam);//将对象转换为JSON格式

        MtCcAgvCommunLogEntity mtCcAgvCommunLogEntity = new MtCcAgvCommunLogEntity();
        Date date = new Date();
        mtCcAgvCommunLogEntity.setCreateTime(date);
        mtCcAgvCommunLogEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcAgvCommunLogEntity.setStatus(0);
        mtCcAgvCommunLogEntity.setContent("agv日志已生成");
        mtCcAgvCommunLogEntity.setTaskCode(reqCode);
        mtCcAgvCommunLogService.save(mtCcAgvCommunLogEntity);

        resultMsg = HttpClientUtil.post(url, param);// 以post形式调用webservice
        JSONObject jsonObject = JSON.parseObject(resultMsg);
        String code = jsonObject.getString("code");
        if (code.equals("0")) {
            mtCcAgvCommunLogEntity.setStatus(1);
            mtCcAgvCommunLogEntity.setContent("agv日志已生成,调用agv接口成功");
            mtCcAgvCommunLogService.update(mtCcAgvCommunLogEntity, new QueryWrapper<MtCcAgvCommunLogEntity>().eq(
                    "task_code", reqCode));
            return R.ok("成功");
        } else if (code.equals("1")) {
            return R.error("参数相关的错误");
        } else {
            return R.error("其他未知错误");
        }
    }

    @PostMapping("/select/material/table/rounte/two")
    @ApiOperation(value = "放料台确认操作2", notes = "放料台确认操作2")
    public R selectMaterialTableRounteTwo(@RequestBody Map<String, Object> params) throws Exception {
        String url = MtCcAGVEnum.AGVURL_callMaterialTouchOff.getCode();
//        Map<String,Object> params = new HashMap<>();
//        params.put("taskType","0");
//        params.put("status","2");
        List<MtCcAreaRouteEntityVo> list = mtCcAreaRouteService.selectRouteCodeByTaskType(params);
//        List<MtCcEquipmentRunInfoEntity> list1 = mtCcEquipmentRunInfoService.selectRunInfoByCreatTimeAsc();
        String reqCode = list.get(0).getTaskCode() + 1;
        String wbCode = "2Y";
//        String taskTyp = "F01";
//        Position position = new Position();
//        position.setPositionCode(list.get(0).getSourceNo());
//        position.setPositionCode("204835AA203655");
//        position.setType("00");
//        Position position1 = new Position();
//        position1.setPositionCode(list.get(0).getDestinationNo());
//        position1.setPositionCode("282655AA228370");
//        position1.setType("00");
//        Object[] positionCodePath = {position,position1};
        AgvParam agvParam = new AgvParam();
        agvParam.setReqCode(reqCode);
        agvParam.setWbCode(wbCode);
//        agvParam.setPositionCodePath(positionCodePath);
        String resultMsg = null;//获取服务端返回的参数
        String param = JSONObject.toJSONString(agvParam);//将对象转换为JSON格式

        MtCcAgvCommunLogEntity mtCcAgvCommunLogEntity = new MtCcAgvCommunLogEntity();
        Date date = new Date();
        mtCcAgvCommunLogEntity.setCreateTime(date);
        mtCcAgvCommunLogEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcAgvCommunLogEntity.setStatus(0);
        mtCcAgvCommunLogEntity.setContent("agv日志已生成");
        mtCcAgvCommunLogEntity.setTaskCode(reqCode);
        mtCcAgvCommunLogService.save(mtCcAgvCommunLogEntity);


        resultMsg = HttpClientUtil.post(url, param);// 以post形式调用webservice
        JSONObject jsonObject = JSON.parseObject(resultMsg);
        String code = jsonObject.getString("code");
        if (code.equals("0")) {
            mtCcAgvCommunLogEntity.setStatus(1);
            mtCcAgvCommunLogEntity.setContent("agv日志已生成,调用agv接口成功");
            mtCcAgvCommunLogService.update(mtCcAgvCommunLogEntity, new QueryWrapper<MtCcAgvCommunLogEntity>().eq(
                    "task_code", reqCode));
            return R.ok("成功");
        } else if (code.equals("1")) {
            return R.error("参数相关的错误");
        } else {
            return R.error("其他未知错误");
        }
    }
}
